package com.matt.repository.impl;

import com.matt.domain.news.SystemNotification;
import com.matt.foundation.query.NewsQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.SystemNotificationNativeRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * *******************************
 * Created by lch.
 * form com.matt
 * on 2018/11/28
 * *******************************
 */
@Service
public class SystemNotificationNativeRepositoryImpl extends BaseNativeSqlRepository implements SystemNotificationNativeRepository {
    @Override
    public List<SystemNotification> findByQuery(NewsQuery query) {
        String querySql = String.format("select id as id,name as name,text as text,description as description,date as date,createTime as createTime,category as category " +
                " from system_notification where true order by date desc");
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize(), query.getPageSize());
        return sqlObjectList(querySql,SystemNotification.class);
    }

    @Override
    public Long findCountByQuery(NewsQuery query) {
        String querySql = String.format("select count(id) from system_notification where true ");
        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }

    @Override
    public SystemNotification findTopNews() {
        String querySql = String.format("select id as id,name as name,text as text,description as description,date as date,createTime as createTime,category as category from system_notification " +
                " order by date desc limit 0,1");
        List<SystemNotification> list = sqlObjectList(querySql,SystemNotification.class);
       if (list.size() > 0)
           return list.get(0) == null ? null : list.get(0);
       return null;
    }
}
